<template>
  <!--  <h1>USER</h1>-->

  <div id="UserManager">

    <el-table :data="users" style="width: 100%">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="userId" label="用户编号" width="180"></el-table-column>
      <el-table-column prop="userName" label="用户名" width="180"></el-table-column>
      <el-table-column prop="userGender" label="用户性别" width="180">
        <template slot-scope="scope">{{ scope.row.userGender === 1 ? '男' : '女' }}</template>
      </el-table-column>
      <el-table-column prop="headerUrl" label="图片地址" width="180"></el-table-column>
      <el-table-column prop="userState" label="用户状态" width="180">
        <template slot-scope="scope">{{ scope.row.userState === 0 ? '正常状态' : '被封禁' }}</template>
      </el-table-column>
      <el-table-column prop="userPhone" label="用户电话" width="180"></el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180"></el-table-column>
      <el-table-column prop="updateTime" label="上一次更新时间" width="180"></el-table-column>
      <el-table-column prop="userAddress" label="用户地址" width="180"></el-table-column>
      <el-table-column prop="userRole" label="用户权限" width="180">
        <template slot-scope="scope">{{ scope.row.userRole === 1 ? '普通用户' : '管理员' }}</template>
      </el-table-column>
      <el-table-column prop="userBalance" label="用户余额" width="180"></el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button type="text" @click="isView(scope.row)">查看</el-button>
          <el-button type="text" @click="isDelete(scope.row)">删除</el-button>
          <el-button type="text" @click="isUpdate(scope.row)">封禁/解禁</el-button>
        </template>
      </el-table-column>

    </el-table>
    <el-pagination
        background layout="prev, pager, next"
        :total="total"
        :current-page="currentPage"
        :page-size="pageSize"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange">
    </el-pagination>
    <el-dialog title="详情界面" :visible.sync="dialogFormVisible">
      <el-descriptions title="用户信息" :column="3" border v-model="seeUser">
        <el-descriptions-item label="用户名" label-class-name="my-label" content-class-name="my-content">
          {{ seeUser.userName }}
        </el-descriptions-item>
        <el-descriptions-item label="手机号">{{ seeUser.userPhone }}</el-descriptions-item>
        <el-descriptions-item label="居住地">{{ seeUser.userAddress }}</el-descriptions-item>
        <el-descriptions-item label="性别">
          <el-tag size="small">{{ seeUser.userGender }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="用户状态">
          <el-tag size="small">{{ seeUser.userState }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="用户权限">
          <el-tag size="small">{{ seeUser.userRole }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="余额">{{ seeUser.userBalance }}</el-descriptions-item>
        <el-descriptions-item label="账号创建时间">{{ seeUser.createTime }}</el-descriptions-item>
        <el-descriptions-item label="上次修改时间">{{ seeUser.updateTime }}</el-descriptions-item>
      </el-descriptions>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
      </div>
    </el-dialog>


  </div>

</template>

<script>
import {deleteUser, getUserList, updateUser} from "@/API/AdminAPI";
import axios from "axios";
import Message from "@/utils/message";

const apiUrl = "http://localhost:8081";
export default {
  name: "UserManager",
  data() {
    return {
      users: [],
      dialogFormVisible: false,
      seeUser: {
        userIdS: "",
        userNameS: "",
        userGenderS: "",
        userUrlS: "",
        userStateS: "",
        userPhoneS: "",
        createTimeS: "",
        updateTimeS: "",
        userAddressS: "",
        userRoleS: "",
        userBalanceS: ""
      },
      options: [{
        value: '选项1',
        label: '封禁'
      }, {
        value: '选项2',
        label: '不封禁'
      },
      ],
      value: '',
      currentPage: 1, // 当前页码
      pageSize: 5, // 每页条数
      total: 0, // 数据总条数

    };
  },
  async mounted() {

    try {
      const result = await getUserList(this.currentPage,this.pageSize);
      if(result.code === 1){
        this.users = result.data;
        this.total = result.data[0].userNum;
        Message.success(this, "用户信息加载成功。");
      }
    } catch (error) {
      Message.error(this, "用户信息加载失败。");
      console.log("用户信息：“");
      console.log(error);
      throw error;
    }
  },
  methods: {
    isUpdate(row) {
      //this.dialogFormVisible = false
      this.$confirm('此操作将修改该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const result = await updateUser(row);
        if(result.code === 1){
          this.$message({
            type: 'success',
            message: '修改成功!'
          });
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        });
      });
    },
    isDelete(row) {

      this.$confirm('此操作将删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteUser(row);
        //axios.delete(`${apiUrl}/manage-user?userId=` + row.userId);
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        });
      });
    },
    isView(row) {
      this.dialogFormVisible = true;
      this.seeUser = row;
      this.seeUser.userGenderS = row.userGender === 1 ? "男" : "女";
      this.seeUser.userStateS = row.userState === 0 ? "正常" : "异常";
      this.seeUser.userRoleS = row.userRole === 0 ? "管理员" : "普通用户";
      this.seeUser.userBalanceS = row.userBalance + "元";
    },
    async handleSizeChange(val) {
      this.pageSize = val;
      try {
        let result = await getUserList(this.currentPage+1,this.pageSize);
        this.users = result.data;
      } catch (error) {
        throw error;
      }
    },
    async handleCurrentChange(val) {
      this.currentPage = val;
      try {
        let result = await getUserList(this.currentPage,this.pageSize);
        this.users = result.data;
      } catch (error) {
        throw error;
      }
    },
  },
};
</script>

<style scoped>

</style>

<style>
.my-label {
  background: #E1F3D8;
}

.my-content {
  background: #FDE2E2;
}
</style>